﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.ServiceProxy.DataServiceRef;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Gauges;

namespace CPPEI.Coolzon.UI.DataPartLib
{
    /// <summary>
    /// DataGaugeDoubleCircularScale.xaml 的交互逻辑
    /// </summary>
    [System.ComponentModel.DisplayName("双表盘仪表盘")]
    [XPartIcon("pack://application:,,,/CPPEI.Coolzon.UI.DataPartLib;component/Images/PartIcons/双指针仪表盘.png")]
    public partial class DataGaugeDoubleCircularScale : DataGaugeBase
    {
        protected const string PRIMARYSCALE = "PRIMARY";
        protected const string SECONDARYSCALE = "SECONDARY";

        protected Dictionary<string, string> dicIndicatorsScaleType = new Dictionary<string, string>();
        public DataGaugeDoubleCircularScale()
        {
            IsMajorTicksEnabled = true;
            IsMinorTicksEnabled = true;
            IsCircularScaleLabelsEnabled = true;

            IsMajor2TicksEnabled = true;
            IsMinor2TicksEnabled = true;
            IsCircular2ScaleLabelsEnabled = true;

            IndicatorsScaleType = new SeriesYAxisType();
            IndicatorsScaleType["series1"] = "Primary";
            IndicatorsScaleType["series2"] = "Secondary";
            Circular1StartAngle = 5;
            Circular1SweepAngle = 170;
            Circular2StartAngle = 185;
            Circular2SweepAngle = 170;

            Major2TicksBackground = new BrushEx(new SolidColorBrush(Colors.Black));
            Minor2TicksBackground = new BrushEx(new SolidColorBrush(Colors.Black));
            Circular2ScaleLabelsFontSize = 12;

            Circular2ScaleLabelsFontColor = new BrushEx(new SolidColorBrush(Colors.Black));
            Circular2ScaleLabelsFontFamily = new FontFamily("微软雅黑");

            Needle2CapBackground = new BrushEx(new SolidColorBrush(Colors.Gray));
            Needle2Background = new BrushEx(new SolidColorBrush(Colors.Orange));

            InitializeComponent();
        }
        protected override void SetDefaultDataResult()
        {
            Random random = new Random();
            ServiceProxy.DataServiceRef.DataSeries[] series = new ServiceProxy.DataServiceRef.DataSeries[2];
            for (int j = 1; j <= series.Length; j++)
            {
                series[j - 1] = new ServiceProxy.DataServiceRef.DataSeries();
                series[j - 1].Id = "series" + j;
                series[j - 1].Name = "series" + j;
                ServiceProxy.DataServiceRef.DataItem[] tmpItems = new ServiceProxy.DataServiceRef.DataItem[1];
                for (int i = 1; i <= 1; i++)
                {
                    tmpItems[i - 1] = new ServiceProxy.DataServiceRef.DataItem();
                    tmpItems[i - 1].Id = "data" + i;
                    tmpItems[i - 1].Name = "data" + i;
                    tmpItems[i - 1].Value = (random.NextDouble() * 100).ToString("F2");
                }
                series[j - 1].DataItems = tmpItems;
            }
            DataResult = new ServiceProxy.DataServiceRef.DataResult();
            DataResult.DataSeries = series;
        }

        protected override void OnApplyXProperty()
        {
            base.OnApplyXProperty();

            Gauge gauge = GetGauge();
            if (gauge != null)
            {
                gauge.CircularScales.Clear();
                CircularScale circularScale1 = new CircularScale();
                SetCircular1Scale(circularScale1);
                circularScale1.MajorTicks = GetMajorTicks();
                circularScale1.MinorTicks = GetMinorTicks();
                circularScale1.ScaleLabels = GetScaleLabels();
                gauge.CircularScales.Add(circularScale1);
                CircularScale circularScale2 = new CircularScale();
                SetCircular2Scale(circularScale2);
                circularScale2.MajorTicks = GetMajor2Ticks();
                circularScale2.MinorTicks = GetMinor2Ticks();
                circularScale2.ScaleLabels = GetScale2Labels();
                gauge.CircularScales.Add(circularScale2);
            }
        }
        protected override void OnDataBinding(DataResult dataResult)
        {
            Gauge gauge = GetGauge();
            gauge.Indicators.Clear();

            foreach (var dataSeries in dataResult.DataSeries)
            {
                string scaleName = GetScaleName(dataSeries.Name);
                foreach (var dataItem in dataSeries.DataItems)
                {
                    Indicator indicator;
                    switch (GetIndicatorType(dataSeries.Name))
                    {
                        case IndicatorTypes.Needle:
                            indicator = new NeedleIndicator();
                            if (scaleName.Equals(PRIMARYSCALE))
                                SetNeedleIndicator((NeedleIndicator)indicator);
                            else
                                SetNeedle2Indicator((NeedleIndicator)indicator);
                            break;
                        case IndicatorTypes.Marker:
                            indicator = new MarkerIndicator();
                            if (scaleName.Equals(PRIMARYSCALE))
                                SetMarkerIndicator((MarkerIndicator)indicator);
                            else
                                SetMarker2Indicator((MarkerIndicator)indicator);
                            break;
                        case IndicatorTypes.Bar:
                            indicator = new BarIndicator();
                            if (scaleName.Equals(PRIMARYSCALE))
                                SetBarIndicator((BarIndicator)indicator);
                            else
                                SetBar2Indicator((BarIndicator)indicator);
                            break;
                        default:
                            indicator = new NeedleIndicator();
                            if (scaleName.Equals(PRIMARYSCALE))
                                SetNeedleIndicator((NeedleIndicator)indicator);
                            else
                                SetNeedle2Indicator((NeedleIndicator)indicator);
                            break;
                    }
                    indicator.ScaleName = scaleName;
                    indicator.LabelText = dataItem.Name;

                    gauge.Indicators.Add(indicator);


                    indicator.Value = 0;
                    indicator.BeginAnimation(NeedleIndicator.ValueProperty, new DoubleAnimation(0, Convertor.ToDouble(dataItem.Value), new Duration(TimeSpan.FromSeconds(XDataAnimationDuration))));
                }
            }
        }

        protected override Gauge GetGauge()
        {
            return _ctlGauge;
        }

        internal override IndicatorTypes GetIndicatorType(string dataSeriesName)
        {
            if (IndicatorsType != null && IndicatorsType.ContainsKey(dataSeriesName))
            {
                switch (IndicatorsType[dataSeriesName].ToUpper())
                {
                    case "NEEDLE": return IndicatorTypes.Needle;
                    case "BAR": return IndicatorTypes.Bar;
                    case "MARKER": return IndicatorTypes.Marker;
                }
            }
            return IndicatorTypes.Needle;
        }

        protected string GetScaleName(string dataSeriesName)
        {
            if (IndicatorsScaleType != null && IndicatorsScaleType.ContainsKey(dataSeriesName))
            {
                switch (IndicatorsScaleType[dataSeriesName].ToUpper())
                {
                    case PRIMARYSCALE: return PRIMARYSCALE;
                    case SECONDARYSCALE: return SECONDARYSCALE;
                }
            }
            return PRIMARYSCALE;
        }

        [Category("其他")]
        [DisplayName("数据串对应表盘类型")]
        [Description("IndicatorsScaleType")]
        [Editor(typeof(XProperty.Editors.XSeriesYAxisTypeEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "IndicatorsScaleType", "数据串对应表盘类型")]
        public SeriesYAxisType IndicatorsScaleType
        {
            get
            {
                return indicatorsScaleType;
            }

            set
            {
                indicatorsScaleType = value;
                Refresh();
            }
        }

        private SeriesYAxisType indicatorsScaleType;
    }
}
